package com.weds.edwx.web;

import com.weds.core.annotation.Logs;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BaseController;
import com.weds.core.base.BasePageSearch;
import com.weds.core.resp.JsonResult;
import com.weds.core.utils.ImageUtils;
import com.weds.core.utils.StringUtils;
import com.weds.edpf.core.constant.ScmParams;
import com.weds.edwx.entity.NEWSEntity;
import com.weds.edwx.entity.NoticeEntity;
import com.weds.edwx.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;


@RestController
@RequestMapping(value = "/noticeController")
@Api(value = "通知管理", description = "通知管理")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;

    @Resource
    private ScmParams scmParams;

    @Value("${weds.scm.scm-url}")
    private String scmUrl;

    @Logs
    @ApiOperation(value = "查询消息列表", notes = "查询消息列表")
    @RequestMapping(value = "/selectByUser", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<NoticeEntity>> selectByUser(@RequestBody BasePageSearch<NoticeEntity> record) {
        setPageHelper(record);
        NoticeEntity entity = record.getSearch();
        if (entity == null) {
            entity = new NoticeEntity();
        }
        List<NoticeEntity> list = noticeService.selectByUser(entity);
        return succMsgData(new BaseCommPager<NoticeEntity>(list));
    }

    @Logs
    @ApiOperation(value = "查询消息详情", notes = "查询消息详情")
    @RequestMapping(value = "/selectByBh", method = RequestMethod.POST)
    public JsonResult selectByBh(@RequestBody NoticeEntity noticeEntity){
        if (noticeEntity.getUserSerial() == null || ("").equals(noticeEntity.getUserSerial())){
            return failMsg("人员序号为空");
        }
        if (noticeEntity.getXh() == null || ("").equals(noticeEntity.getXh())){
            return failMsg("编号为空");
        }
        // 查询通知详情
        NoticeEntity result =  noticeService.selectByBh(noticeEntity);
        if (result == null) {
            return failMsg("通知不存在");
        }
        if (result.getIsRead() == 0) { // 当前通知状态未读, 修改通知状态
            noticeService.update(noticeEntity);
        }
        if (!StringUtils.isBlank(result.getTopImg())) { // 顶部图片
            try {
                if (!StringUtils.isBlank(scmParams.getScmUrl())) {
                    result.setTopImg(scmParams.getScmUrl() + File.separator + "photo" + File.separator + "graphic" + File.separator + result.getTopImg() + ".jpg");
                } else {
                    result.setTopImg(ImageUtils.imgToBase64(scmParams.getRootPath() + File.separator + "wwwroot" + File.separator + "photo" + File.separator + "graphic" + File.separator + result.getTopImg() + ".jpg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!StringUtils.isBlank(result.getEndImg())) { // 底部图片
            try {
                if (!StringUtils.isBlank(scmParams.getScmUrl())) {
                    result.setEndImg(scmParams.getScmUrl() + File.separator + "photo" + File.separator + "graphic" + File.separator + result.getEndImg() + ".jpg");
                } else {
                    result.setEndImg(ImageUtils.imgToBase64(scmParams.getRootPath() + File.separator  + "wwwroot" + File.separator + "photo" + File.separator + "graphic" + File.separator + result.getEndImg() + ".jpg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return succMsg("查询成功",result);
    }
}
